/**
 * Created by Administrator on 2017/2/25 0025.
 */
'use strict';

angular.module('App')
    .factory('Util',['$http','$rootScope',function ($http,$rootScope,$scope) {

        return {
            /*
            * 模态框
            * */
            model: function (message, callback) {
                // warn = warn || false;
                var node = $('<div id="__alert__"></div>');
                node.css({
                    height: '44px',
                    lineHeight: '45px',
                    textAlign: 'center',
                    background: '#f0ad4e',
                    color: '#fff',
                    position: 'fixed',
                    fontSize: '14px',
                    bottom: 0,
                    left: 0,
                    right: 0,
                    zIndex:100000
                });
                // if (warn) {
                //     node.css('background', '#4cae4c');
                // } else {
                //     node.css('background', '#f0ad4e');
                // }
                node.html(message);
                $('body').append(node);
                node.css('left', ($('body').width() - node[0].clientWidth) / 2 + 'px').slideDown();
                window.setTimeout(function () {
                    node.slideUp(300, function () {
                        if (typeof callback === 'function') {
                            callback();

                        }
                        $('#__alert__').remove();
                    });
                }, 3000);
            },
            checkTel:function(telNo){
                return /^1[34578]\d{9}$/.test(telNo);
            },
            timeFormat:function(){   //封装 时间
                var getDateFormat = function(){
                    var options = options || {};
                    options.sign = options.sign || 'yyyy-MM-dd HH:mm:ss';
                    var _complete = function(n){
                        return (n>9) ? n : '0' + n;
                    }
                    var d = new Date();
                    var year = d.getFullYear();
                    var month = _complete(d.getMonth()+1);
                    var day = _complete(d.getDate());
                    var hours = _complete(d.getHours());
                    var minutes = _complete(d.getMinutes());
                    var second = _complete(d.getSeconds());
                    var result = options.sign;
                    result = result.replace('yyyy', year);
                    result = result.replace('MM', month);
                    result = result.replace('dd', day);
                    result = result.replace('HH', hours);
                    result = result.replace('mm', minutes);
                    result = result.replace('ss', second);
                    return result;
                }
                return getDateFormat();
            },
            /*
             * 放大图片模态框
             * */
            bigImg: function ( message ) {
                // warn = warn || false;
                var node = $('<div id="bigImg"></div>');
                var nodeImg = $('<img />');
                nodeImg.attr('src',message);
                node.append(nodeImg);
                $('body').append(node);
                node.click(function(){
                    node.remove();
                })
            },
            /*
            * highChart   有pie 饼的走势图    等。。。。。。。 line
            * */
            gethighChart:function( elem,type,title,data1,data2 ){

                elem.highcharts({
                    chart: {
                        //alignTicks: false,
                        type: type
                    },
                    title:{
                        text: title
                    },
                    xAxis: {
                        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
                    },
                    yAxis: [{
                        title: {
                            text: 'tianming'
                        },
                        gridLineWidth: 0
                    }, {
                        title: {
                            text: 'mingtian'
                        },
                        opposite: true
                    }],
                    legend: {
                        layout: 'vertical',
                        backgroundColor: '#FFFFFF',
                        floating: true,
                        align: 'left',
                        x: 100,
                        verticalAlign: 'top',
                        y: 70
                    },
                    tooltip: {
                        formatter: function () {
                            return '<b>' + this.series.name + '</b><br/>' +
                                this.x + ': ' + this.y;
                        }
                    },
                    plotOptions: {
                    },
                    series: [{
                        data:
                        data1
                            // [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
                    }, {
                        data:
                        data2,

                            //[129.9, 271.5, 306.4, 29.2, 544.0, 376.0, 435.6, 348.5, 216.4, 294.1, 35.6, 354.4],
                        yAxis: 1
                    }]
                });
            },
            /*
            * 键盘函数封装
            * */
            keyFun:function ( callback ) {
                document.onkeydown=function(event){
                   // var e = event || window.event || arguments.callee.caller.arguments[0];
                   var e = event || window.event ;
                    if(e && e.keyCode==27){ // 按 Esc
                         //要做的事情
                      }
                     if(e && e.keyCode==113){ // 按 F2
                            //要做的事情

                     }
                      if(e && e.keyCode==13){ // enter 键
                          console.log(160);
                          var a = 1;

                     }

                };
            },

            /*
            *
            * highcharts  走势图 area
            *
            * */
            areaHighcharts:function (elme,title,type) {
                $(function () {
                    $.getJSON('https://data.jianshukeji.com/jsonp?filename=json/usdeur.json&callback=?', function (data) {
                        elme.highcharts({
                            chart: {
                                zoomType: 'x'
                            },
                            title: {
                                text: title
                            },
                            subtitle: {
                                text: document.ontouchstart === undefined ?
                                    '鼠标拖动可以进行缩放' : '手势操作进行缩放'
                            },
                            xAxis: {
                                type: 'datetime',
                                dateTimeLabelFormats: {
                                    millisecond: '%H:%M:%S.%L',
                                    second: '%H:%M:%S',
                                    minute: '%H:%M',
                                    hour: '%H:%M',
                                    day: '%m-%d',
                                    week: '%m-%d',
                                    month: '%Y-%m',
                                    year: '%Y'
                                }
                            },
                            tooltip: {
                                dateTimeLabelFormats: {
                                    millisecond: '%H:%M:%S.%L',
                                    second: '%H:%M:%S',
                                    minute: '%H:%M',
                                    hour: '%H:%M',
                                    day: '%Y-%m-%d',
                                    week: '%m-%d',
                                    month: '%Y-%m',
                                    year: '%Y'
                                }
                            },
                            yAxis: {
                                title: {
                                    text: '汇率'
                                }
                            },
                            legend: {
                                enabled: false
                            },
                            plotOptions: {
                                area: {
                                    fillColor: {
                                        linearGradient: {
                                            x1: 0,
                                            y1: 0,
                                            x2: 0,
                                            y2: 1
                                        },
                                        stops: [
                                            [0, Highcharts.getOptions().colors[0]],
                                            [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
                                        ]
                                    },
                                    marker: {
                                        radius: 2
                                    },
                                    lineWidth: 1,
                                    states: {
                                        hover: {
                                            lineWidth: 1
                                        }
                                    },
                                    threshold: null
                                }
                            },
                            series: [{
                                type: type,
                                name: '美元兑欧元',
                                data: data
                            }]
                        });
                    });
                });
            }


        }
    }])